/*
Learning from Law Enforcement
Libor Dusek and Christian Traxler

Do-file producing regression results and figures in the paper and (online) appendix

*************************************
*** DESCRIPTION AND PRELIMINARIES
**************************************

*** This do file: RD_07.do
    RD estimates for responses on 'unmonitored' road between speed cameras (enforcement cutoff)

*** Required packages:
    rdrobust

*** The input dataset:
    Dusek_Traxler_RD_interim_road.dta
	
*** Output is stored in output directories: OUTPUT/RD_TAB  OUTPUT/RD_FIG , and OUTPUT/RD_temp
	Names of outpupt files correspond to the Table/Figure numbers in the paper and (online) appendix
	
*** List of Figures and Tables produced:
    => Table C.3
    See also Section C of the ReadMe File.
*/

cap clear
set more off

/* Set directories */
global dir C:/yourdir/
cd    $dir
global datadir $dir/datasets/
global outputdir outfiles/
capture mkdir ${outputdir}
capture mkdir ${outputdir}/RD_TAB
capture mkdir ${outputdir}/RD_FIG
capture mkdir ${outputdir}/RD_temp

cap log close
log using    $dir/${outputdir}/log_RD_01.txt, replace

/* Load Supplementary Data (defined for A = F = 4) */
use $datadir/Dusek_Traxler_RD_interim_road.dta, clear

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* I.A   Define Variables & Estimation Sample
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

/* Define assignment speed, centered around enforcement cutoff */
gen speed_Z = maxspeed - 14

/* Log of time traveled in between camera zone 1 and (entry into) zone 4*/
gen     log_time_inter=log(time_inter)

/* Derive Speed on unmonitored road:
   The road betweeen the exit point of camera zone 1 and the entry point of
   camera zone 4 is 1080 meters. Hence, we devide the length by time (in sec)
   and multiply by 60^2 (sec => hour) / 1,000 (m => km) = 3.6. This yields a
   proxy for speed (in km/h) */

gen      speed_inter = 1080 / time_inter
replace  speed_inter = 3.6 * speed_inter

/* Sample 1 : cross-section of cars  */
gen     ESTSAMPLE=0
replace ESTSAMPLE=1 if period_post==1 & id_record==id_record_post_min

/*Average Speed on rides at unmonitored road */
bysort id:  gegen xxspeed_m    = mean(speed_inter) if period_post==1
by id:  gegen speed_inter_m    = mean(xxspeed_m)
drop xx*

/* Top Speed */
by id: gegen xxtime_p90      = pctile(speed_inter) if period_post==1 ,p(90)
by id: gegen speed_inter_p90 = mean(xxtime_p90)
drop xx*

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* I.B   Reduced Form Estimates (Cut 1), Car Level => Tab. C3, Col.1 + 2
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

rdrobust speed_inter_m  speed_Z if ESTSAMPLE==1, all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su speed_inter_m if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
  local y_l = `r(mean)'
  local rel = 100 * e(tau_bc)/`r(mean)'
 qui outreg2 using ${outputdir}/RD_TAB/Tab_C3.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4)   replace

rdrobust speed_inter_p90  speed_Z if ESTSAMPLE==1, all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su speed_inter_p90 if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
  local y_l = `r(mean)'
  local rel = 100 * e(tau_bc)/`r(mean)'
 qui outreg2 using ${outputdir}/RD_TAB/Tab_C3.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4)  append

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* I.C   Reduced Form Estimates (Cut 1), RIDE Level  => Tab. C3, Col.3 + 4
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

/* Sample 2 : ride level estimates  */
replace ESTSAMPLE=0
replace ESTSAMPLE=1 if period_post==1

foreach Y of varlist speed_inter log_time_inter {
 rdrobust `Y'  speed_Z if ESTSAMPLE==1, all p(1) bwselect(mserd) vce(nncluster id)  kernel(tri)
  local bw = h_mserd
  qui su `Y'  if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
  local y_l = `r(mean)'
  local rel = 100 * e(tau_bc)/`r(mean)'
 qui outreg2 using ${outputdir}/RD_TAB/Tab_C3.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4)  append
}


log close
